package com.cszjzx.member.service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import com.cszjzx.member.dao.MasterDAO;
import com.cszjzx.member.dao.MasterDAOImpl;
import com.cszjzx.member.po.Master;


public class MasterServiceImpl implements MasterService {

	private MasterDAO masterDao = null;
	
	public MasterServiceImpl() {
		try {
			masterDao = new MasterDAOImpl();
		} catch (IOException e){
			e.printStackTrace();
		}
	}
	
	
	@Override
	public void alterPassword(Master master) {
		masterDao.update(master);
	}

	@Override
	public Master getMasterById(String masterId,int module) {
		Master master = null;
		try {
			master = masterDao.find(masterId,module);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return master;
	}
	
	@Override
	public List<Master> getMasters(int module) {
		List<Master> masters = null;
		
		try {
			masters = masterDao.findAll(module);
			for (int i = 0 ; i < masters.size() ; i ++) {
				if (masters.get(i).isIssuper()) {
					masters.remove(i);
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return masters;
	}


	//娣诲姞涓�涓猰aster
	@Override
	public boolean addMaster(String masterId, String password,int module,int departmentid) {
		Master master = new Master(masterId,password,false,module);
		master.setDepartmentid(departmentid);
		try {
			masterDao.add(master);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


	//鏍规嵁masterid鍒犻櫎master
	@Override
	public boolean deleteMaster(String masterId) {
		try {
			 masterDao.delete(masterId);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	

}
